#include<sys/types.h>
#include<sys/socket.h>
#include<stdio.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<stdlib.h>
#include<pthread.h>
#include<string.h>
#include <errno.h>



int main(void)
{
    int sockfd;
    int len;
    struct sockaddr_in address;
    int result;
    pthread_t tid;

    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    address.sin_family = AF_INET;
    //注意：inet_addr函数返回的已经是按网络字节序存储的网络地址了，所以不能再加htonl了
    address.sin_addr.s_addr = inet_addr("127.0.0.1");
    address.sin_port = htons(11021);
    len = sizeof(address);

    result = connect(sockfd, (struct sockaddr*)&address, len);
    if (-1 == result)
    {
        printf("error = %d\n", errno);

        perror("oops: client");
        exit(1);
    }
    else 
    {
        printf("connect success.\n");
    }

    int writelen = write(sockfd, "hello, world", strlen("hello, world"));
    printf("write len %d\n", writelen);
    sleep(10);
    writelen = write(sockfd, "bye", strlen("bye"));
    printf("write len %d\n", writelen);
    sleep(20);
    
    close(sockfd);

    return 0;
}
